﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Reflection;
using System.IO;

namespace Acsys
{
    public partial class SettingsForm : Form
    {
        MySqlConnection m_connection;
        IMainForm m_mailForm;
        Crypto m_crypto = new Crypto();

        public SettingsForm(IMainForm mainForm)
        {
            InitializeComponent();
            m_mailForm = mainForm;
            buttonCreateDB.Enabled = false;
            //buttonCreateDB.Enabled = m_mailForm.CurrentUser != null;
            comboBoxConnection.DataSource = new string[]{
            "MySQL ODBC 5.1 Driver","MySQL ODBC 3.51 Driver"
            };
            if (File.Exists("setting.conf"))
            {
                using (StreamReader file = new StreamReader(@"setting.conf"))
                {
                    comboBoxConnection.Text = m_crypto.Decrypt(file.ReadLine());
                    textBoxHost.Text = m_crypto.Decrypt(file.ReadLine());
                    textBoxUser.Text = m_crypto.Decrypt(file.ReadLine());
                    textBoxPassword.Text = m_crypto.Decrypt(file.ReadLine());
                }
            }
        }

        private void SettingsForm_Load(object sender, EventArgs e)
        {

        }

        private void buttonConnectionTest_Click(object sender, EventArgs e)
        {
            //m_connection = new MySqlConnection(@"Driver={" + comboBoxConnection.SelectedItem.ToString() + "};server=" + textBoxHost.Text +
                  // ";uid=" + textBoxUser.Text + ";pwd=" + textBoxPassword.Text);
            m_connection = new MySqlConnection(@"server=" + textBoxHost.Text +
                   ";uid=" + textBoxUser.Text + ";pwd=" + textBoxPassword.Text);
            try
            {
                m_connection.Open();//;database=acsys_db
                //m_connection.Close();
                using (StreamWriter file = new StreamWriter(@"setting.conf"))
                {
                    file.WriteLine(m_crypto.Encrypt(comboBoxConnection.SelectedItem.ToString()));
                    file.WriteLine(m_crypto.Encrypt(textBoxHost.Text));
                    file.WriteLine(m_crypto.Encrypt(textBoxUser.Text));
                    file.WriteLine(m_crypto.Encrypt(textBoxPassword.Text));
                }
               // m_connection = new MySqlConnection(@"Driver={" + comboBoxConnection.SelectedItem.ToString() + "};server=" + textBoxHost.Text +
                //   ";database=acsys_db;uid=" + textBoxUser.Text + ";pwd=" + textBoxPassword.Text);
                MessageBox.Show("Connection is OK!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                buttonCreateDB.Enabled = true;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            finally
            {
                m_connection.Close();
            }
        }

        private void buttonCreateDB_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_mailForm.CurrentUser.CODE != "ADMIN")
                {
                    MessageBox.Show("Please contact your admin user.", "Confirmation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                if (MessageBox.Show("Are you sure to create new DB? it will drop the current DB!", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
                if (m_connection == null)
                {
                    MessageBox.Show("Please test the connection first!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                this.Cursor = Cursors.WaitCursor;
                m_connection.Open();
                Assembly assm = Assembly.GetExecutingAssembly();
                StreamReader str = new StreamReader(assm.GetManifestResourceStream("Acsys.createDB.sql"));
                string sqls = str.ReadToEnd();
                string[] sql = sqls.Split(';');

                foreach (string q in sql)
                {
                    if (q.Trim() == string.Empty) continue;
                    MySqlCommand cmd = new MySqlCommand(q.Trim(), m_connection);
                    cmd.ExecuteNonQuery();
                }
                m_connection.Close();
                MessageBox.Show("New DB is created succesfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Cursor = Cursors.Default;
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void buttonBackUp_Click(object sender, EventArgs e)
        {

        }
    }
}
